// 子应用生命周期处理，换将变量设置

import { performScript, performScriptForEval } from './performScript'
// import { SnapShotSandbox } from './snapShotSandbox'
import { ProxySandbox } from './proxySandbox'

function isCheckLifeCycle(lifecycle) {
  return lifecycle && lifecycle.bootstrap && lifecycle.mount && lifecycle.unmount
}

export const sandBox = (app, script) => {
  const proxy = new ProxySandbox()

  if (!app.proxy) {
    app.proxy = proxy
  }

  // 标识当前环境为微前端环境
  window.__MICRO__WEB__ = true

  // 运行js文件
  const lifecycle = performScript(script, app.name, app.proxy.proxy)

  // lifecycle中如果有需要的生命周期就挂载到lifecycle上
  if (isCheckLifeCycle(lifecycle)) {
    app.bootstrap = lifecycle.bootstrap
    app.mount = lifecycle.mount
    app.unmount = lifecycle.unmount
  }
}
